package com.marktotop.controller.business;


import com.marktotop.entity.business.Vaccine;
import com.marktotop.entity.business.VaccineDevolution;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ISupplierService;
import com.marktotop.service.IVaccinationSiteService;
import com.marktotop.service.IVaccineDevolutionService;
import com.marktotop.service.IVaccineService;
import com.marktotop.vo.business.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器 - 疫苗下发
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-16
 */
@Api(tags = "业务模块-疫苗下发接口")
@RestController
@RequestMapping("/business/devolution")
public class VaccineDevolutionController {

    @Autowired
    private IVaccineDevolutionService devolutionService;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IVaccineService vaccineService;

    @Autowired
    private IVaccinationSiteService vaccinationSiteService;

    /**
     * 分页展示所有疫苗下发记录列表，并进行模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param devolutionVO
     * @return
     */
    @ApiOperation(value = "下发记录", notes = "疫苗下发记录列表,并进行模糊查询")
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public ResultBean findVaccineDevolutionList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       VaccineDevolutionVO devolutionVO ){
        return devolutionService.findVaccineDevolutionList(pageNum,pageSize,devolutionVO);
    }


    /**
     * 添加疫苗下发记录信息
     *
     * @param devolution
     * @return
     */
    @ApiOperation(value = "疫苗下发", notes = "添加疫苗下发记录信息")
    @RequiresPermissions({"devolution:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean addVaccineDevolutionRecord(@RequestBody @Validated VaccineDevolution devolution) throws SystemException {
        if(!devolutionService.addVaccineDevolutionRecord(devolution)){
            return ResultBean.error().message("添加疫苗下放记录信息失败！");
        }
        return ResultBean.ok().message("添加疫苗下放记录信息成功！");
    }


    /**
     * 获取所有疫苗供应商列表，供疫苗下发使用
     *
     * @return
     */
    @ApiOperation(value = "获取所有供应商列表", notes = "获取所有疫苗供应商列表，供疫苗下发使用")
    @RequestMapping(value = "/findAllSuppliersList",method = RequestMethod.GET)
    public ResultBean findAllSuppliersList(){
        List<VaccineSupplierVO> vaccineSupplierList = supplierService.findVaccineSupplierList();
        if (null == vaccineSupplierList){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("VaccSupplier",vaccineSupplierList);
    }


    /**
     * 获取所有疫苗列表，（带供应商id、供应商名称）供疫苗下发使用
     *
     * @return
     */
    @ApiOperation(value = "获取所有疫苗列表", notes = "获取所有疫苗列表，供疫苗下发使用")
    @RequestMapping(value = "/findAllVaccinesList",method = RequestMethod.GET)
    public ResultBean findAllVaccinesList(){
        List<VaccineToDevolutionVO> vaccinesList = vaccineService.findAllVaccinesForOther();
        if (null == vaccinesList){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("VaccinesList",vaccinesList);
    }


    /**
     * 获取所有接种点列表，供疫苗下发使用
     *
     * @return
     */
    @ApiOperation(value = "获取所有接种点列表", notes = "获取所有疫苗接种点列表，供疫苗下发使用")
    @RequestMapping(value = "/findAllVaccSites",method = RequestMethod.GET)
    public ResultBean findAllVaccSites(){
        List<VaccSitesVO> vaccSitesList = vaccinationSiteService.findVaccinationSitesList();
        if (null == vaccSitesList){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("VaccSites",vaccSitesList);
    }


    /**
     * 编辑 疫苗下发记录
     *
     * @return
     */
    @ApiOperation(value = "编辑下发记录", notes = "返回要编辑的疫苗下发对象信息")
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    @RequiresPermissions({"devolution:edit"})
    public ResultBean editVaccineDevolutionRecord(@PathVariable Long id){
        VaccineDevolutionVO devolutionRecord = devolutionService.editDevolutionRecord(id);
        if (null == devolutionRecord){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("devolution",devolutionRecord);
    }


     /**
      * 更新 疫苗下发记录
      *
      * @return
      */
     @ApiOperation(value = "更新下发记录", notes = "更新疫苗下发对象信息")
     @RequestMapping(value = "/update/",method = RequestMethod.PUT)
     public ResultBean updateVaccineDevolutionRecord(@RequestBody @Validated VaccineDevolution devolution){
         if (!devolutionService.updateById(devolution)){
             return ResultBean.error().message("更新下发记录操作失败！");
         }
         return ResultBean.ok().message("更新下发记录操作成功！");
     }


    /**
     * 删除 疫苗下发记录
     *
     * @return
     */
    @ApiOperation(value = "删除下发记录", notes = "删除疫苗下发对象信息")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean deleteVaccineDevolutionRecord(@PathVariable Long id){
        if (!devolutionService.removeById(id)){
            return ResultBean.error().message("删除下发记录操作失败！");
        }
        return ResultBean.ok().message("删除下发记录操作成功！");
    }

}
